﻿//-----------------------------------------------------------------------------------------//
//-     Writen by Raymond Honderdors
//-     2013-05-14
//-
//-     Code can be used as is, no warrenty
//-     https://solutionmanagementki.codeplex.com/
//-----------------------------------------------------------------------------------------//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AnalysisServices;
namespace SMK_SSAS
{
    /// <summary>
    /// 
    /// </summary>
    public class AMO_Database_Accounts
    {
        private AMO_Database lDatabase = null;
        private Microsoft.AnalysisServices.Account amoaccount = null;
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public AMO_Database AMODatabase()
        {
            if (lDatabase != null)
            {

                return lDatabase;

            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Microsoft.AnalysisServices.Account amodbaccount()
        {
            if (amoaccount != null)
            {

                return amoaccount;

            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Datebase"></param>
        /// <param name="AccountID"></param>
        public void SetAMODatabaseAccount(AMO_Database Datebase, string AccountID)
        {

            if (Datebase != null)
            {
                lDatabase = Datebase;
                AMO_Server Server = Datebase.AMOServer();
                if (Server.Isconnected())
                {
                    if (Datebase.amodatabase().Accounts.Contains(AccountID))
                    {
                        amoaccount = Datebase.amodatabase().Accounts.Find(AccountID);
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Boolean HasAliases()
        {
            if (amoaccount != null)
            {
                if (amoaccount.Aliases.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public SortedList<string, AMO_Key> Aliases()
        {
            SortedList<string, AMO_Key> result = new SortedList<string, AMO_Key>();
            foreach (string db in amoaccount.Aliases)
            {
                AMO_Key amok = new AMO_Key();
                amok.ID = db;
                amok.Name = db;
                result.Add(amoaccount.AccountType + "\\" + db, amok);
            }
            return result;
        }
    }
}
